﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;



namespace CRM.Web.SurveyManage
{
    public partial class SurveyModify : Common.PageBase
    {
        T_SURVEY_BLL sBll = new T_SURVEY_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pageDataInit();
            }
        }

        #region 页面初始化数据绑定
        #endregion

        private void pageDataInit()
        {
            string _surveyid = String.IsNullOrEmpty(Request.QueryString["SURVEY_ID"]) ? "" : Request.QueryString["SURVEY_ID"].Trim();
            if (!String.IsNullOrEmpty(_surveyid))
            {
                T_SURVEY_Entity entity = sBll.GetEntity("SURVEY_ID", _surveyid);
                PageHelper.SetControlValue<T_SURVEY_Entity>(this.Page, entity);
                lblTitle.Text = "问卷信息修改";
            }
            else
            {
                txtSDATE.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                txtGHID.Text = _userLoginInfo.UserEntity.USERID.ToString();
                txtCREATEPEOPLE.Text = _userLoginInfo.UserEntity.USERNAME.ToString();
                lblTitle.Text = "问卷信息添加";
                btnSurveyItems.Visible = false;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            T_SURVEY_Entity entity = new T_SURVEY_Entity();
            if (txtSNAME.Text == "")
            {
                PageHelper.ShowMessage(this.Page, "问卷名称不能为空！");
                return;
            }
            entity = sBll.GetEntity("SURVEY_ID", txtSURVEY_ID.Text);
            if (entity == null)
            {
                entity = sBll.GetEntity("SNAME", txtSNAME.Text);
                if (entity == null)
                {
                    entity = new T_SURVEY_Entity();
                    PageHelper.GetControlValue<T_SURVEY_Entity>(this.Page, ref entity);
                    entity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                    entity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                    long rel = sBll.Insert(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "添加成功！", "SurveyList.aspx");
                    }
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, "问卷名称已经存在，请核实后再进行增加");
                }
            }
            else
            {
                PageHelper.GetControlValue<T_SURVEY_Entity>(this.Page, ref entity);
                long rel = sBll.Update(entity);
                if (rel > 0)
                {
                    PageHelper.ShowMessage(this.Page, "修改成功！", "SurveyList.aspx");
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, "修改失败！");
                }
            }
        }

        protected void btnSurveyItems_Click(object sender, EventArgs e)
        {
            Response.Redirect(String.Format("SurveyItemsList.aspx?Survey_ID={0}&Survey_NAME={1}", txtSURVEY_ID.Text,txtSNAME.Text));
        }
    }
}
